﻿using System.Data.SqlClient;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Modules._2DoTasks.Components.Entities;
using DotNetNuke.Modules._2DoTasks.Components.ExceptionHandling;
using DotNetNuke.Modules._2DoTasks.Components.Interfaces;
using DotNetNuke.Modules._2DoTasks.Data;

namespace DotNetNuke.Modules._2DoTasks.Components.Controllers
{
    /// <summary>
    /// Controller object with business logic for TaskComments
    /// </summary>
    public class TaskCommentController : IRepository<_TaskComment, int>
    {
        private UnitOfWork _uof;
        private int _taskId;

        #region IRepository<_TaskComment> Members

        public TaskCommentController(UnitOfWork uof, int taskId)
        {
            _uof = uof;
            this._taskId = taskId;
        }

        /// <summary>
        /// Gets the TaskComment with the passed id
        /// </summary>
        /// <param name="TaskCommentId">the TaskComment id</param>
        /// <param name="additionalFields">not used in TaskComment entity</param>
        /// <returns>returns a TaskComment object</returns>
        public _TaskComment Get(int TaskCommentId, params object[] additionalFields)
        {
            return CBO.FillObject<_TaskComment>(DataProvider.Instance().GetTaskComment(TaskCommentId, this._taskId, this._uof.CurrentUser.UserID));
        }

        /// <summary>
        /// Returns all TaskComments
        /// </summary>
        /// <returns>retuns list of TaskComments</returns>
        public System.Collections.Generic.List<_TaskComment> GetAll()
        {
            return CBO.FillCollection<_TaskComment>(DataProvider.Instance().GetAllTaskComments(this._taskId, this._uof.CurrentUser.UserID));
        }

        /// <summary>
        /// Adds a new entity
        /// </summary>
        /// <param name="entity">entity to create</param>
        public void Add(_TaskComment entity)
        {
            try
            {
                entity.TaskId = this._taskId;
                entity.CreationUserId = this._uof.CurrentUser.UserID;
                entity.ModuleID = this._uof.ModuleId;
                entity.Id = DataProvider.Instance().Add(entity, this._uof.PortalId);
            }
            catch (SqlException exc)
            {
                if (exc.Message.Contains("fk_TskCm_TaskId"))
                    throw new TaskCommentValidationException("Task not found.",
                        SharedEnum.CrudState.Insert,
                        (int)InsertStatus.TASKNOTFOUND);

                if (exc.Message.Contains("USERCANNOTMODIFYTASK"))
                    throw new TaskCommentValidationException("The user making the change has no acess hover the project.",
                        SharedEnum.CrudState.Insert,
                        (int)InsertStatus.USERCANNOTMODIFYTASK);

                throw exc;
            }
        }

        /// <summary>
        /// Updates a TaskComment
        /// </summary>
        /// <param name="entity">TaskComment to update</param>
        public void Update(_TaskComment entity)
        {
            try
            {
                entity.TaskId = this._taskId;
                entity.LastChangeUserId = this._uof.CurrentUser.UserID;
                entity.ModuleID = this._uof.ModuleId;

                DataProvider.Instance().Update(entity, this._uof.PortalId);
            }
            catch (SqlException exc)
            {
                if (exc.Message.Contains("fk_TskCm_TaskId"))
                    throw new TaskCommentValidationException("Task not found.",
                        SharedEnum.CrudState.Update,
                        (int)UpdateStatus.TASKNOTFOUND);

                if (exc.Message.Contains("USERCANNOTMODIFYTASK"))
                    throw new TaskCommentValidationException("The user making the change has no acess.",
                        SharedEnum.CrudState.Update,
                        (int)UpdateStatus.USERCANNOTMODIFYTASK);

                throw exc;
            }
        }

        /// <summary>
        /// Deletes an existing TaskComment
        /// </summary>
        /// <param name="entity">TaskComment to delete</param>
        public void Delete(_TaskComment entity)
        {
            try
            {
                entity.TaskId = this._taskId;
                entity.ModuleID = this._uof.ModuleId;
                DataProvider.Instance().Delete(entity, this._uof.PortalId, this._uof.CurrentUser.UserID);
            }
            catch (SqlException exc)
            {
                if (exc.Message.Contains("USERCANNOTMODIFYTASK"))
                    throw new TaskCommentValidationException("The user making the change has no acess.",
                        SharedEnum.CrudState.Delete,
                        (int)DeleteStatus.USERCANNOTMODIFYTASK);

                throw exc;
            }
        }

        #endregion IRepository<_TaskComment> Members

        #region "Crud Enums"

        public enum InsertStatus
        {
            TASKNOTFOUND = -1,
            USERCANNOTMODIFYTASK = -2
        }

        public enum UpdateStatus
        {
            TASKNOTFOUND = -1,
            USERCANNOTMODIFYTASK = -2
        }

        public enum DeleteStatus
        {
            USERCANNOTMODIFYTASK = -1
        }

        #endregion "Crud Enums"
    }
}